package com.easy.custompasswordedit.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.easy.custompasswordedit.R;

/**
 * Created by Sigthp-P-Department on 2018/5/2.
 */

public class CustomPassWrodInputLayout extends RelativeLayout implements View.OnClickListener {
    private final Context mContext;
    private CustomPassWrodListener mListener=null;

    public CustomPassWrodInputLayout(Context context) {
        this(context,null);
    }

    public CustomPassWrodInputLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomPassWrodInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext=context;
        initAttrs(attrs);
        inflate(context, R.layout.input_layout, this);
        setChildViewOnclick(this);
    }

    private void initAttrs(AttributeSet attrs) {
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.CustomPassWrodInputLayout);
        typedArray.recycle();
    }
    /**
     * 设置键盘子View的点击事件
     **/
    private void setChildViewOnclick(ViewGroup parent) {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            // 不断的递归设置点击事件
            View view = parent.getChildAt(i);
            if (view instanceof ViewGroup) {
                setChildViewOnclick((ViewGroup) view);
                continue;
            }
            view.setOnClickListener(this);
        }
    }


    @Override
    public void onClick(View view) {
        String number = ((TextView) view).getText().toString();
        if (!TextUtils.isEmpty(number)) {
            if("X".equals(number)){
                if (mListener != null) {
                    // 回调
                    mListener.delete();
                }
            }else{
                if (mListener != null) {
                    // 回调
                    mListener.onClick(((TextView) view));
                }
            }

        }

    }

    public interface CustomPassWrodListener{
        public void onClick(TextView textView);
        void delete();
    }

    public void setCustomPassWrodListener(CustomPassWrodListener listener){
        this.mListener=listener;
    }
}
